package org.jeecg.modules.demo.applyapartemployee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.demo.applyapartemployee.entity.ZnzfApplyApartEmployee;
import org.jeecg.modules.demo.applyapartemployee.mapper.ZnzfApplyApartEmployeeMapper;
import org.jeecg.modules.demo.applyapartemployee.service.IZnzfApplyApartEmployeeService;
import org.jeecg.modules.demo.applyinfo.entity.ZnzfDetermineApart;
import org.jeecg.modules.demo.applyinfo.mapper.ZnzfDetermineApartMapper;
import org.jeecg.modules.demo.base.entity.ZnzfEmployee;
import org.jeecg.modules.demo.base.mapper.ZnzfEmployeeMapper;
import org.jeecg.modules.system.entity.SysUserDepart;
import org.jeecg.modules.system.mapper.SysUserDepartMapper;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 单位报名人员
 * @Author: jeecg-boot
 * @Date:   2022-03-19
 * @Version: V1.0
 */
@Service
public class ZnzfApplyApartEmployeeServiceImpl extends ServiceImpl<ZnzfApplyApartEmployeeMapper, ZnzfApplyApartEmployee> implements IZnzfApplyApartEmployeeService {

    @Resource
    private ZnzfApplyApartEmployeeMapper znzfApplyApartEmployeeMapper;

    @Resource
    private ZnzfDetermineApartMapper znzfDetermineApartMapper;

    @Resource
    private SysUserDepartMapper sysUserDepartMapper;

    @Autowired
    private ZnzfEmployeeMapper znzfEmployeeMapper;

    @Override
    public List<ZnzfApplyApartEmployee> searchsByCheckDetermineApartId(String check_determine_apart_id) {
        LambdaQueryWrapper<ZnzfApplyApartEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfApplyApartEmployee::getDetermineApartId,check_determine_apart_id);
        return znzfApplyApartEmployeeMapper.selectList(queryWrapper);
    }

    /**
     * 让某人入围
     *
     * @param id
     * @return
     */
    @Override
    public int shortlistSb(String id) {
        ZnzfApplyApartEmployee employee = new ZnzfApplyApartEmployee();
        employee.setIsShortlisted(1);
        employee.setId(id);
        return znzfApplyApartEmployeeMapper.updateById(employee);
    }

    /**
     * 可以提交报名,提交报名申请,往单位报名表插入一条数据
     *
     * @param applyId
     * @param uid
     * @return
     */
    @Override
    public int submitApply(String applyId, String uid) {
        //获取eid
        LambdaQueryWrapper<ZnzfEmployee> employeeWrapper = new LambdaQueryWrapper<>();
        employeeWrapper.eq(ZnzfEmployee::getUserId,uid);
        String eid = znzfEmployeeMapper.selectOne(employeeWrapper).getId();
        //获取departId
        List<SysUserDepart> userDeparts = sysUserDepartMapper.getUserDepartByUid(uid);
        String departId = userDeparts.get(0).getDepId();
        //获取框定单位id
        LambdaQueryWrapper<ZnzfDetermineApart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnzfDetermineApart::getApartId,departId).eq(ZnzfDetermineApart::getApplyListId,applyId);
        String determineDepartId = znzfDetermineApartMapper.selectOne(wrapper).getId();

        //查询是否已报名
        LambdaQueryWrapper<ZnzfApplyApartEmployee> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(ZnzfApplyApartEmployee::getApartId,departId)
                .eq(ZnzfApplyApartEmployee::getDetermineApartId,determineDepartId)
                .eq(ZnzfApplyApartEmployee::getUserId,uid)
                .eq(ZnzfApplyApartEmployee::getEmployeeId,eid);
        ZnzfApplyApartEmployee isExist = znzfApplyApartEmployeeMapper.selectOne(wrapper2);
        if(isExist != null)
            return 2;
        ZnzfApplyApartEmployee employee = new ZnzfApplyApartEmployee();
        employee.setEmployeeId(eid);
        employee.setUserId(uid);
        employee.setApartId(departId);
        employee.setDetermineApartId(determineDepartId);
        employee.setApplyTime(new Date());
        //获取分数
        LambdaQueryWrapper<ZnzfEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfEmployee::getUserId, uid);
        ZnzfEmployee one = znzfEmployeeMapper.selectOne(queryWrapper);
        Double score = null;
        if (one != null) {
            score = one.getLatestScore();
        }
        employee.setScore(score);
        return znzfApplyApartEmployeeMapper.insert(employee);
    }
}
